package com.sevlet.setting;

import com.Bean.EmployeeTwo;
import com.dao.EmployeeTwoDao;
import com.dao.impl.EmployeeTwoDaoImpl;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.sevlet.BaseSevlet;
import com.util.JsonUtil;
import com.util.PageUtil;
import com.util.ProxyUtil;
import org.apache.commons.beanutils.BeanUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;
import java.util.List;

@WebServlet("/EmployeeServlet")
public class EmployeeServlet extends BaseSevlet {

    public void query(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        EmployeeTwoDao employeeDao = new EmployeeTwoDaoImpl();
        EmployeeTwo employee = new EmployeeTwo();
        resp.setContentType("text/html;charset=utf-8");
        JsonUtil jsonUtil = new JsonUtil();
        GsonBuilder gsonBuilder = new GsonBuilder();
        gsonBuilder.setDateFormat("yyyy-MM-dd");
        Gson gson = gsonBuilder.create();
        PrintWriter out = null;


        //System.out.println(req.getParameter("bookInfo"));

        try {
            out = resp.getWriter();


        } catch (IOException e) {
            e.printStackTrace();
        }
        String curentPage =   req.getParameter("currentPage");
        String value =req.getParameter("value");
        String text =   req.getParameter("text");
        employee.setEid(text);
        employee.setEstatus(value);
        employee.setEname(text);
        try {
            PageUtil<EmployeeTwo> employeePageUtil =  employeeDao.queryDataBypage(Integer.parseInt(curentPage),15,employee);

            jsonUtil.setCode(0);
            jsonUtil.setMsg("成功");
            System.out.println(employeePageUtil);
            jsonUtil.setData(employeePageUtil);

        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        }  finally {
            out.print(gson.toJson(jsonUtil));
        }
    }

    public void queryText(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        EmployeeTwoDao employeeDao = new EmployeeTwoDaoImpl();

        EmployeeTwo employee = new EmployeeTwo();
        resp.setContentType("text/html;charset=utf-8");
        JsonUtil jsonUtil = new JsonUtil();
        GsonBuilder gsonBuilder = new GsonBuilder();
        gsonBuilder.setDateFormat("yyyy-MM-dd");
        Gson gson = gsonBuilder.create();
        PrintWriter out = null;
        String value = req.getParameter("value");
        String text = req.getParameter("text");
        employee.setEstatus(value);
        employee.setEid(text);
        employee.setEname(text);
        try {
            out = resp.getWriter();
        } catch (IOException e) {
            e.printStackTrace();
        }
        try {
            List<EmployeeTwo> employees = employeeDao.queryDataForText(employee);
            System.out.println(employees.toString());
            jsonUtil.setCode(0);
            jsonUtil.setMsg("成功");

            jsonUtil.setData(employees);

        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        }  finally {
            out.print(gson.toJson(jsonUtil));
        }
    }

    public void insertData(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        //调用Dao的删除方法
        //BooksDao booksDao = (BooksDao)ProxyUtil.getDaoProxy(new BooksDaoImpl());
        EmployeeTwoDao employeeDao = new EmployeeTwoDaoImpl();
        EmployeeTwoDao daoProxy = (EmployeeTwoDao) ProxyUtil.getDaoProxy(employeeDao);

        JsonUtil jsonUtil = new JsonUtil();
        try {
            EmployeeTwo employee = new EmployeeTwo();

            System.out.println(req.getParameter("eid"));

            //将客户端传递过来的请求参数封装到bookBean对象中
            //要求：参数名必需与bookBean中的属性名有对应关系
            System.out.println("反射前的BookBean对象中的属性值为:"+employee);
            BeanUtils.populate(employee,req.getParameterMap());
            System.out.println("反射后的BookBean对象中的属性值为:"+employee);

            int res =  daoProxy.insert(employee);

            jsonUtil.setCode(0);
            jsonUtil.setMsg("成功");

        } catch (SQLException e) {
            e.printStackTrace();
            jsonUtil.setCode(1);
            jsonUtil.setMsg("系统错误:"+e.getMessage());
        }
        catch (Exception e){
            e.printStackTrace();
            jsonUtil.setCode(1);
            jsonUtil.setMsg("系统错误:"+e.getMessage());
        }

        GsonBuilder gsonBuilder = new GsonBuilder();
        Gson gson =  gsonBuilder.create();

        String jsonStr =  gson.toJson(jsonUtil);


        resp.setContentType("text/html;charset=utf-8");
        PrintWriter out = resp.getWriter();
        out.print(jsonStr);
    }


    public void delete(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        EmployeeTwoDao employeeDao = new EmployeeTwoDaoImpl();
        EmployeeTwoDao daoProxy = (EmployeeTwoDao) ProxyUtil.getDaoProxy(employeeDao);

        EmployeeTwo employee = new EmployeeTwo();
        resp.setContentType("text/html;charset=utf-8");
        JsonUtil jsonUtil = new JsonUtil();
        GsonBuilder gsonBuilder = new GsonBuilder();
        gsonBuilder.setDateFormat("yyyy-MM-dd");
        Gson gson = gsonBuilder.create();
        PrintWriter out = null;
        String eid = req.getParameter("eid");
        System.out.println(eid);
        employee.setEid(eid);

        try {
            out = resp.getWriter();
        } catch (IOException e) {
            e.printStackTrace();
        }
        try {
            int res = daoProxy.deleteById(eid);
            jsonUtil.setCode(0);
            jsonUtil.setMsg("成功");

        }  catch (SQLException throwables) {
            throwables.printStackTrace();
        }   finally {
            out.print(gson.toJson(jsonUtil));
        }
    }

    public void updateDate(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        //调用Dao的删除方法
        //BooksDao booksDao = (BooksDao)ProxyUtil.getDaoProxy(new BooksDaoImpl());
        EmployeeTwoDao employeeDao = new EmployeeTwoDaoImpl();
        EmployeeTwoDao daoProxy = (EmployeeTwoDao) ProxyUtil.getDaoProxy(employeeDao);

        JsonUtil jsonUtil = new JsonUtil();
        try {
            EmployeeTwo employee = new EmployeeTwo();

            //将客户端传递过来的请求参数封装到bookBean对象中
            //要求：参数名必需与bookBean中的属性名有对应关系
            System.out.println("反射前的BookBean对象中的属性值为:"+employee);
            BeanUtils.populate(employee,req.getParameterMap());
            System.out.println("反射后的BookBean对象中的属性值为:"+employee);

            int res =  daoProxy.update(employee);

            jsonUtil.setCode(0);
            jsonUtil.setMsg("成功");

        } catch (SQLException e) {
            e.printStackTrace();
            jsonUtil.setCode(1);
            jsonUtil.setMsg("系统错误:"+e.getMessage());
        }
        catch (Exception e){
            e.printStackTrace();
            jsonUtil.setCode(1);
            jsonUtil.setMsg("系统错误:"+e.getMessage());
        }

        GsonBuilder gsonBuilder = new GsonBuilder();
        Gson gson =  gsonBuilder.create();

        String jsonStr =  gson.toJson(jsonUtil);


        resp.setContentType("text/html;charset=utf-8");
        PrintWriter out = resp.getWriter();
        out.print(jsonStr);
    }

}
